<template>
  <el-dialog
    title="添加关键字"
    :visible.sync="visible"
    width="500px"
    append-to-body
    :close-on-click-modal="false"
  >
    <el-form ref="editForm" :model="editForm" :rules="rules" label-width="100px">
      <el-form-item label="添加关键字" prop="aiName">
        <el-select
          v-model="editForm.aiName"
          placeholder="请添加关键字"
          multiple
          filterable
          allow-create
          style="width: 100%"
          @change="changeName"
        >
          <el-option v-for="(i, k) in aiNameList" :key="k" :label="i" :value="i" />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveOrUpdateAiName } from "@/api/system/ai";
export default {
  data() {
    return {
      visible: false, // 显示弹窗
      editForm: {}, // 编辑表单
      aiNameList: [],
      rules: { aiName: [{ required: true, message: "请输入关键字", trigger: "blur" }] },
    };
  },
  methods: {
    // 打开弹窗
    init(row) {
      this.visible = true;
      this.aiNameList = row.aiName;
      this.editForm = {
        id: row.id,
        aiName: row.aiName,
      };
    },
    // 调整下拉框的值
    changeName(v) {
      v.forEach((i) => {
        if (this.aiNameList.indexOf(i) == -1) {
          this.aiNameList.push(i);
        }
      });
    },
    // 新增
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateAiName(this.editForm).then((res) => {
            this.$modal.msgSuccess(res.msg);
            this.$emit("refresh", true);
          });
        }
      });
    },
    // 关闭弹窗
    cancel() {
      this.visible = false;
    },
  },
};
</script>

<style></style>
